<?php
if($_SERVER["SERVER_NAME"]=='qiyue.zhongsou.com' || $_SERVER["SERVER_NAME"]=='qiyuenew.zhongsou.com'){
ini_set('session.save_handler', 'redis');
ini_set('session.save_path', 'tcp://103.7.222.41:6399');
}
session_start();
$php_path = dirname(__FILE__) . '/';
require_once 'JSON.php';
require_once $php_path.'../../../include/lxbase.class.php';
require_once $php_path.'../../../include/wx.class.php';
$php_url = dirname($_SERVER['PHP_SELF']) . '/';
//定义允许上传的文件扩展名
$ext_arr = array(
	'image' => array('gif', 'jpg', 'jpeg', 'png'),
	'flash' => array('swf', 'flv'),
	'media' => array('swf', 'flv', 'mp3', 'wav', 'wma', 'wmv', 'mid', 'avi', 'mpg', 'asf', 'rm', 'rmvb'),
	'file' => array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'htm', 'html', 'txt', 'zip', 'rar', 'gz', 'bz2'),
);
//最大文件大小
$max_size = 1000000;


//有上传文件时
if (empty($_FILES) === false) {
	//原文件名
	$file_name = $_FILES['imgFile']['name'];
	//服务器上临时文件名
	$tmp_name = $_FILES['imgFile']['tmp_name'];
	//文件大小
	$file_size = $_FILES['imgFile']['size'];
	//检查文件名
	if (!$file_name) {
		alert("请选择文件。");
	}
	//检查是否已上传
	if (@is_uploaded_file($tmp_name) === false) {
		alert("临时文件可能不是上传文件。");
	}
	//检查文件大小
	if ($file_size > $max_size) {
		alert("上传文件大小超过限制。");
	}
	//获得文件类型
	$dir_name = empty($_GET['dir']) ? 'image' : trim($_GET['dir']);
	//获得文件扩展名
	$temp_arr = explode(".", $file_name);
	$file_ext = array_pop($temp_arr);
	$file_ext = trim($file_ext);
	$file_ext = strtolower($file_ext);
	//检查扩展名
	if (in_array($file_ext, $ext_arr[$dir_name]) === false) {
		alert("上传文件扩展名是不允许的扩展名。\n只允许" . implode(",", $ext_arr[$dir_name]) . "格式。");
	}
	//$tempname="kdeditor_".date("YmdHis").rand(1,9).".".$file_ext;
	//$s = new SaeStorage();
	//$file_url=$s->upload('temp',$tempname,$tmp_name);
	$s = new Wx();
	$s->userinfo = $_SESSION["userinfo"];
	$tempurl = $s->settempfile($_FILES['imgFile']);
	$file_url = $s->getmediaurl($tempurl);
	header('Content-type: text/html; charset=UTF-8');
	$json = new Services_JSON();
	echo $json->encode(array('error' => 0, 'url' => $file_url));
	exit;
}

function alert($msg) {
	header('Content-type: text/html; charset=UTF-8');
	$json = new Services_JSON();
	echo $json->encode(array('error' => 1, 'message' => $msg));
	exit;
}
?>